package com.briup.jd2407cms.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.briup.jd2407cms.domain.User;
import com.briup.jd2407cms.domain.extend.UserExtend;
import com.briup.jd2407cms.result.Result;
import com.briup.jd2407cms.result.ResultCode;
import com.briup.jd2407cms.service.UserService;
import com.briup.jd2407cms.utils.Md5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/auth/user")
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation("新增/注册用户")
    @PostMapping("/save")
    public Result<String> saveUser(@RequestBody User user){
        userService.addUser(user);
        return Result.success("新增成功");
    }

    @ApiOperation("根据id查询用户信息")
    @GetMapping("/queryById/{id}")
    public Result<User> findUserById(@PathVariable Long id){
        return Result.success(userService.getUserById(id));
    }
    @ApiOperation("根据条件分页查询所有用户并带其角色 ")
//    @ApiImplicitParams({
//            @ApiImplicitParam()
//    })
    @GetMapping("/query")
    public Result<IPage<UserExtend>> findUsersWithRole(@RequestParam Integer pageNum,
                                                       @RequestParam Integer pageSize,
                                                       Integer isVip, Integer roleId,
                                                        String status, String username
                                                 ){
        IPage<UserExtend> allWithRoleByPage = userService.getAllWithRoleByPage(pageNum, pageSize, isVip, roleId, status, username);
        return Result.success(allWithRoleByPage);
    }
    @ApiOperation("修改用户状态")
    @PutMapping("/setVip/{id}")
    public Result<?> changeVipStatus(@PathVariable Long id){
        userService.setVipStatus(id);
        return Result.success("会员状态修改成功");
    }
    @ApiOperation("更新用户信息")
    @PutMapping("/update")
    public Result<?> editUserInfo(@RequestBody User user){
        userService.editUserInfo(user);
       return Result.success("更新用户信息成功");
    }
    @ApiOperation("删除用户，可批量")
    @DeleteMapping("/deleteByBatch/{ids}")
    public Result<?> removeUsers(@PathVariable List<Long> ids){
        userService.removeBatch(ids);
        return Result.success("删除成功");
    }

    @ApiOperation("查询用户信息")
    @GetMapping("/info")
    public Result<User> findUser(HttpServletRequest request){
        // 从请求中取出拦截器里保存的userId
        String userId =(String) request.getAttribute("userId");
        // 调用业务层中，根据id查询用户的方法
        User user = userService.getById(userId);
        return Result.success(user);
    }

    @GetMapping("/getAllUser")
    public Result<List<User>> findAllUser(){
        return Result.success(userService.list(null));
    }

}
